package user

import (
	"context"
	"go-zero-all-demos/apps/user/rpc/userservice"

	"go-zero-all-demos/apps/api/internal/svc"
	"go-zero-all-demos/apps/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
	return &GetUserListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserListLogic) GetUserList(req *types.UserListRequest) (resp *types.UserListResponse, err error) {
	response, err := l.svcCtx.UserRPC.GetUserList(l.ctx, &userservice.UserListRequest{
		Keyword:  req.Keyword,
		Page:     req.Page,
		PageSize: req.PageSize,
		LastSize: req.LastSize,
	})
	if err != nil {
		return nil, err
	}

	users := make([]types.UserInfo, len(response.Users))
	for i, user := range response.Users {
		users[i] = types.UserInfo{
			Id:       user.Id,
			Name:     user.Name,
			Password: user.Password,
		}
	}
	resp = &types.UserListResponse{
		Total: response.Total,
		Users: users,
		Page:  response.Page,
		Pages: response.Pages,
	}
	return resp, nil
}
